<template >
    <div>
        <el-dialog :before-close="beforeClose" center :title="props.isAdd ? '添加菜单' : '修改菜单'" v-model="isShow">
            <el-form :model="menuForm" :rules="rules" ref="ruleFormRef" label-width="120px" class="demo-ruleForm"
                :size="formSize" status-icon>
                <el-form-item label="菜单名称" prop="title" :label-width="formLabelWidth">
                    <el-input v-model="menuForm.title" autocomplete="off" />
                </el-form-item>
                <el-form-item label="上级菜单" prop="pid" :label-width="formLabelWidth">
                    <el-select @change="changePid" v-model="menuForm.pid" placeholder="请选择上级菜单">
                        <el-option label="顶级菜单" :value="0" />
                        <el-option v-for="item in menulist" :key="item.id" :label="item.title" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="菜单类型" :label-width="formLabelWidth">
                    <el-radio disabled v-model="menuForm.type" :label="1">目录</el-radio>
                    <el-radio disabled v-model="menuForm.type" :label="2">菜单</el-radio>
                </el-form-item>
                <el-form-item v-if="menuForm.type == 1" label="菜单图标" :label-width="formLabelWidth">
                    <el-select v-model="menuForm.icon" autocomplete="off">
                        <el-option v-for="item in iconlist" :key="item" :value="item">
                            <el-icon>
                                <component :is="item"></component>
                            </el-icon>
                        </el-option>
                    </el-select>

                </el-form-item>
                <el-form-item v-else :label-width="formLabelWidth" label="菜单地址">
                    <el-select v-model="menuForm.url">
                        <el-option v-for="item in         indexRoutes" :key="item.path" :value="item.path"
                            :label="item.meta.title">
                        </el-option>
                    </el-select>

                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="menuForm.status" active-color="#13ce66" inactive-color="#ff4949"
                        :active-value="1" :inactive-value="2" autocomplete="off"></el-switch>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="cancel(ruleFormRef)">取消</el-button>
                    <el-button type="primary" @click="comfirm(ruleFormRef)">确定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts" setup>
// 引入封装好的接口
import { getMenuadd, getMenuInfo, getMenuEdit } from '../../request/api'
// 引入路由路线
import { indexRoutes } from '../../router'
// 引入仓库
import { useStore } from 'vuex'
import { reactive, ref, defineProps, defineEmits, computed, defineExpose } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
const ruleFormRef = ref<FormInstance>()
const formSize = ref('default')
const formLabelWidth = '140px'
const store = useStore();
// 获取列表
const menulist = computed(() => store.getters["menu/getMenu"])
console.log(menulist, 'menulist')
const getMenu = () => {
    store.dispatch("menu/getMenuAction")
}
// 组件一加载触发行动
getMenu();
// 封装一个icon
let iconlist = ["Briefcase", "Camera", "ChatDotRound", "Coordinate", "DCaret"];
let props = defineProps({
    isShow: Boolean,
    isAdd: Boolean
})
// props.isAdd = true
const emit = defineEmits(['cancel'])

//封装一个beforeClose处理方法
const beforeClose = (done: () => {}) => {
    done();
    cancel(ruleFormRef.value)
}
// 取消
const cancel = (formEl: FormInstance | undefined) => {
    console.log(formEl, "formElformElformEl");
    emit("cancel", false);
    if (!formEl) return;
    formEl.resetFields();
    menuForm.value = {
        id: '',
        pid: 0,//上级分类编号  注意：顶级菜单-0
        title: '',//菜单名称
        icon: '',//图标
        type: 1,//类型 1目录2菜单
        url: '',//菜单地址
        status: 1,//状态   1正常2禁用   number类型
    };
};
// 封装一个切换上级菜单与目录
let changePid = () => {
    menuForm.value.type = menuForm.value.pid == 0 ? 1 : 2
}
const indexRouters: any = reactive({ indexRoutes })
const menuForm = ref({
    id: '',
    pid: 0,//上级分类编号  注意：顶级菜单-0
    title: '',//菜单名称
    icon: '',//图标
    type: 1,//类型 1目录2菜单
    url: '',//菜单地址
    status: 1,//状态   1正常2禁用   number类型
})
// 封装一个表单清空
// 重置表单验证规则
// const resetForm = (formEl: FormInstance | undefined) => {

// }
// 获取一个事件
const lookup = (e: any) => {
    // console.log(e,'eeee')s
    getMenuInfo({ id: e }).then((res: any) => {
        console.log(res, '获取一条数据');
        if (res.code == 200) {
            menuForm.value = res.list
            menuForm.value.id = e
        }
    })
}
defineExpose({
    lookup
})
// 封装一个添加接口
const comfirm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            ElMessage.success("验证成功");
            // console.log(props.isAdd, 'isAdd');
            if (props.isAdd) {
                console.log(menuForm, '表单');
                getMenuadd(menuForm.value).then((res) => {
                    console.log(res, '添加成功');
                    if (res.code == 200) {
                        ElMessage.success(res.msg);
                        cancel(formEl);
                        getMenu();
                    } else {
                        ElMessage.error(res.msg)
                    }
                })
            } else {
                getMenuEdit(menuForm.value).then((res) => {
                    console.log(res, '添加成功');
                    if (res.code == 200) {
                        ElMessage.success(res.msg);
                        cancel(formEl);
                        getMenu();
                    } else {
                        ElMessage.error(res.msg)
                    }
                })

            }
        } else {
            console.log('error submit!', fields)
        }
    })
}

const rules = reactive<FormRules>({
    title: [
        { required: true, message: '请输入菜单名称', trigger: 'blur' },
        { min: 2, max: 5, message: '菜单名称长度为2到5', trigger: 'blur' },
    ],
    pid: [{ required: true, message: "请选择上级菜单", trigger: "change" },],
})

</script>
<style lang="">
    
</style>